package com.riven.bilibilidemo.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.miguelcatalan.materialsearchview.MaterialSearchView;
import com.riven.bilibilidemo.R;
import com.riven.bilibilidemo.activity.MainActivity;
import com.riven.bilibilidemo.fragment.discover.DiscoverFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页面的fragment
 */
public class HomeFragment extends Fragment {
    private List<Fragment> list_fragment = new ArrayList<>();
    private View inflate;
    private MaterialSearchView materialSearchView;
    private LinearLayout linearLayout;
    private String[] str = new String[]{
            "直播","推荐","番剧","分区","关注","发现"
    };
    private ViewPager viewPager;
    private TabLayout tabLayout;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.fragment_home, container, false);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initView();
        initSearchView();
        initViewPagerData();
        initListener();
    }


    /*
        初始化监听
     */
    private void initListener() {
        //TabLayout的切换监听
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //切换tablayout的时候做相应的操作
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        /**
         * viewPager的监听
         */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 为viewPager设置适配器
     */
    private void initViewPagerData() {
        viewPager.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return list_fragment.size();
            }

            @Override
            public Fragment getItem(int position) {
                return list_fragment.get(position);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return str[position];

            }
        });
        tabLayout.setupWithViewPager(viewPager);
    }

    /**
     * 添加子fragment
     */
    private void initData() {
//        list_fragment.add("添加子fragment")；
        list_fragment.add(new LiveFragment());//直播页面
        list_fragment.add(new RecommendFragment());//推荐页面
        list_fragment.add(new BangumiFragment());//番剧页面
        list_fragment.add(new PartitionFragment());//分区页面
        list_fragment.add(new FollowFragment());//关注页面
        list_fragment.add(new DiscoverFragment());//发现页面
    }

    private void initView() {
        //设置ToolBar
        Toolbar toolbar = (Toolbar) inflate.findViewById(R.id.toolbar);
        viewPager = (ViewPager) inflate.findViewById(R.id.home_viewPager);
        materialSearchView = (MaterialSearchView) inflate.findViewById(R.id.search_view);
        linearLayout = (LinearLayout) inflate.findViewById(R.id.navigation_layout);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Activity activity = getActivity();
                if (activity instanceof MainActivity) {
                    ((MainActivity) activity).toggleDrawer();
                }
            }
        });
        toolbar.setTitle("");
        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);//替换系统的actionBar

        //设置TabLayout
        tabLayout = (TabLayout) inflate.findViewById(R.id.tabLayout);

        tabLayout.setTabMode(TabLayout.MODE_FIXED);//tab不能滚动，平分屏幕宽度



//        //添加tablayout的title
//        for (int i = 1; i < str.length; i++) {
//            tabLayout.addTab(tabLayout.newTab().setText(str[i]));
//        }


    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        menu.clear();
        inflater.inflate(R.menu.menu_main, menu);
        // 设置SearchViewItemMenu
        MenuItem item = menu.findItem(R.id.id_action_search);
        materialSearchView.setMenuItem(item);
    }

    private void initSearchView() {

        //初始化SearchBar
        materialSearchView.setVoiceSearch(false);
        materialSearchView.setCursorDrawable(R.drawable.custom_cursor);
        materialSearchView.setEllipsize(true);
        materialSearchView.setSuggestions(getResources().getStringArray(R.array.query_suggestions));
        materialSearchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {

                return false;
            }


            @Override
            public boolean onQueryTextChange(String newText) {

                return false;
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        switch (id) {
            case R.id.id_action_game:
                //游戏中心
                Toast.makeText(getActivity(),"游戏中心",Toast.LENGTH_SHORT).show();
                break;

            case R.id.id_action_download:
                //离线缓存
                Toast.makeText(getActivity(),"离线缓存",Toast.LENGTH_SHORT).show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    public boolean isOpenSearchView() {

        return materialSearchView.isSearchOpen();
    }


    public void closeSearchView() {

        materialSearchView.closeSearch();
    }

}
